package cdbft

import (
	"blockchain/public/function"
)


func prepareDelegate(nodeSeq int){
	var hash [32]byte
	var prepare prepareMessage
	prepare.nodeSeq=nodeSeq
	h:=pre_prepareData(nodeSeq)
	if hash!=h{
		hash=h
	}
	prepare.tradesHash=hash
	prepare.sign=function.SignHash(prepare.tradesHash,prepare.nodeSeq)
	var flagPrepared=make(chan bool)
	go nodeSendPre_prepareDelagate(nodeSeq,prepare,flagPrepared)
	<-flagPrepared
	//fmt.Println(prepare.tradesHash)
}

func nodeSendPre_prepareDelagate(nodeSeq int,prepare prepareMessage,flagPrepared chan bool){
	for i:=0;i<groupNum;i++{
		select {
		case chanRecPrepare[delegate[i]]<-prepare:
			FlagPrepared[nodeSeq][delegate[i]]<-true
			close(FlagPrepared[nodeSeq][delegate[i]])
		default:FlagPrepared[nodeSeq][delegate[i]]<-false
			close(FlagPrepared[nodeSeq][delegate[i]])
		}
	}
	flagPrepared<-true
	close(flagPrepared)

}


